package com.hangzhou.xyj.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.hangzhou.xyj.model.DianDramaModel;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.model.MessageModel;
import com.hangzhou.xyj.model.RecordModel;
import com.hangzhou.xyj.model.SimpleUserModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.utils.constants.Constants;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * @author : neo.duan
 * @date : 	  2016年2月23日
 * @desc :
 */

public class DataHelper extends OrmLiteSqliteOpenHelper {
    private static final String TAG = DataHelper.class.getSimpleName();

    public DataHelper(Context context) {
        super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            Log.d(TAG, "create user table");
            TableUtils.createTable(connectionSource, UserModel.class);
            TableUtils.createTable(connectionSource, RecordModel.class);
            TableUtils.createTable(connectionSource, DianDramaModel.class);
            TableUtils.createTable(connectionSource, SimpleUserModel.class);
            TableUtils.createTable(connectionSource, MessageModel.class);
            TableUtils.createTable(connectionSource, GroupPushMsgModel.class);
        } catch (SQLException e) {
            Log.e(TAG, "create database error");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2,
                          int arg3) {
        try {
            TableUtils.dropTable(connectionSource, UserModel.class, true);
            TableUtils.dropTable(connectionSource, RecordModel.class, true);
            TableUtils.dropTable(connectionSource, DianDramaModel.class, true);
            TableUtils.dropTable(connectionSource, SimpleUserModel.class, true);
            TableUtils.dropTable(connectionSource, MessageModel.class, true);
            TableUtils.dropTable(connectionSource, GroupPushMsgModel.class, true);
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.e(TAG, "updateShowList database error");
        }
    }

    @Override
    public void close() {
        super.close();
    }
}
